package top.codecrab.eblog.template;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.codecrab.eblog.common.templates.DirectiveHandler;
import top.codecrab.eblog.common.templates.TemplateDirective;
import top.codecrab.eblog.entity.Comment;
import top.codecrab.eblog.service.CommentService;
import top.codecrab.eblog.vo.UserVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 本周前4最多评论
 */
@Component
public class CommentMaxTemplate extends TemplateDirective {

    @Autowired
    private CommentService commentService;

    @Override
    public String getName() {
        return "commentMaxUsers";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        List<UserVo> commentUsers = commentService.getTopCommentUser();

        List<Map<String, Object>> result = commentUsers.stream()
            .map(commentUser -> {
                Map<String, Object> user = new HashMap<>();
                user.put("username", commentUser.getUsername());
                user.put("avatar", commentUser.getAvatar());
                List<Comment> commentList = commentUser.getCommentList();
                user.put("currWeekCommentCount", commentList == null ? 0 : commentList.size());
                return user;
            })
            .collect(Collectors.toList());

        handler.put(RESULTS, result).render();
    }
}
